package edu.ucsd.cse110;

import java.util.Calendar;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import edu.ucsd.cse110.model.Profile;

/**
 * TODO: Need documentation.
 */
public class SettingsActivity extends PreferenceActivity {
	private static boolean bFacebook;

	public static String getName(Context context) {
		return PreferenceManager.getDefaultSharedPreferences(context)
				.getString("LastName", "");
	}

	public static boolean isAutoFBLoginChecked() {
		return bFacebook;
	}

	private Profile mProfile;

	/**
	 * Called when the activity is first created.
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.layout.settings);

		mProfile = new Profile();

		// Get the app's shared preferences
		SharedPreferences preference = PreferenceManager
				.getDefaultSharedPreferences(this);

		// Get the value for the run
		bFacebook = preference.getBoolean("facebook", true);
		mProfile.setLastName(preference.getString("profile_name", "N/A"));
		mProfile.setGender(preference.getString("Gender", "N/A"));
		mProfile.setBirthday(Calendar.getInstance().getTime());
		mProfile.setHeight(preference.getFloat("Height", 0));
		mProfile.setWeight(preference.getFloat("Weight", 0));

		// Update
		SharedPreferences.Editor editor = preference.edit();
		editor.putBoolean("facebook", bFacebook);
		editor.putString("FirstName", mProfile.getFirstName());
		editor.putString("LastName", mProfile.getLastName());
		editor.putString("Gender", mProfile.getGender());
		editor.putLong("Birthday", mProfile.getBirthday().getTime());
		editor.putFloat("Height", mProfile.getHeight());
		editor.putFloat("Weight", mProfile.getWeight());
		editor.commit();
		/*
		 * bFacebook = preference.getBoolean("facebook", true); sProfileName =
		 * preference.getString("profile_name",
		 * "Error: please reenter your profile name"); // necessary sGender =
		 * preference.getString("profile_gender",
		 * "Error: please reselect your gender"); // necessary sBirthday =
		 * preference.getString("profile_bday",
		 * "Error: please reenter your birthday"); // necessary sHeight =
		 * preference.getString("profile_height",
		 * "Error: please reenter your height"); // necessary sWeight =
		 * preference.getString("profile_weight",
		 * "Error: please reenter your weight"); // necessary
		 * 
		 * Profile.editProfile(sProfileName, sGender, sBirthday, sHeight,
		 * sWeight);// send // it // to // the // profile // class
		 */
		// debugging
		/*
		 * android.util.Log.e("The profile name is: ",sProfileName); //testing
		 * android.util.Log.e("The profile gender is: ",sGender); //testing
		 * android.util.Log.e("The profile birthday is: ",sBirthday); //testing
		 * android.util.Log.e("The profile height is: ",sHeight); //testing
		 * android.util.Log.e("The profile weight is: ",sWeight); //testing//
		 */
		// Toast.makeText(this, profile.getProfileName(),
		// Toast.LENGTH_SHORT).show(); //testing
	}

}